<?php

declare(strict_types=1);

/*结算时所需公用方法*/

//用户结算显示
use App\Model\Guard;
use App\Model\PayCall;
use App\Model\UserSuperVip;
use App\Utils\MemcacheClient;

function showBalanceForUser($paycall, $anchor_info)
{
    $data['other_uid'] = $anchor_info['uid'];
    $data['other_nickname'] = $anchor_info['nickname'];
    $data['other_head_image'] = $anchor_info['head_image'];
    $data['price'] = $paycall['price'];
    $data['amount'] = $paycall['fare'];
    if ($paycall['end_time'] > 0) {
        $data['total_time'] = $paycall['end_time'] - $paycall['start_time'] - $paycall['free_video_time'];
        $data['total_time'] <= 0 && $data['total_time'] = 0;

    } else {
        $data['total_time'] = 0;
    }
    $data['account'] = $paycall['user_coin'];
    $data['role'] = 'user';
    $data['free_video_time'] = $paycall['free_video_time'];
    $data['card_id'] = $paycall['card_id'];
    return $data;
}

//主播结算显示
function showBalanceForAnchor($paycall, $user_info)
{
    $data['other_uid'] = $user_info['id'];
    $data['other_nickname'] = $user_info['nickname'];
    $data['other_head_image'] = $user_info['head_image'];
    $data['price'] = $paycall['price'] * $paycall['rate'];
    $data['amount'] = $paycall['profit'];
    if ($paycall['end_time'] > 0) {
        $data['total_time'] = $paycall['end_time'] - $paycall['start_time'] - $paycall['free_video_time'];
        $data['total_time'] <= 0 && $data['total_time'] = 0;
    } else {
        $data['total_time'] = 0;
    }
    $data['account'] = $paycall['anchor_account'];
    $data['role'] = 'anchor';
    $data['free_video_time'] = $paycall['free_video_time'];
    $data['card_id'] = $paycall['card_id'];
    return $data;
}

function isGiftVersion()
{
    return true;
}


//根据通话结束时记录的extend编号来获取“结束原因”
function getReason($extend)
{
    $extent = intval($extend);
    if (in_array($extend, array(11, 12, 21, 22))) {
        $desc = '用户余额不足，通话结束~';
    } elseif (in_array($extend, array(13, 14, 15, 23, 24, 25))) {
        $desc = '对方网络超时，通话结束';
    } elseif (in_array($extend, array(31, 41, 1, 2))) {
        $desc = '对方放弃本次通话';
    } elseif (in_array($extend, array(32, 33, 34, 35, 42, 43, 44, 45))) {
        $desc = '对方挂断了通话';
    } elseif ($extend == 51) {
        $desc = '双方网络超时，通话结束';
    } elseif ($extend == 52) {
        $desc = '抱歉，通话长时间不能接通';
    } elseif ($extend == 16) {
        $desc = '免费通话结束';
    } elseif ($extend == 17) {
        if (in_array(\Hyperf\Context\Context::get('cvf')['version_code'], [393, 394])) {
            $desc = 'SVIP免费通话结束';
        } else {
            $desc = '公爵免费通话结束';
        }
    } else {
        $desc = '本次通话已结束';
    }
    return $desc;
}


// 计算剩余免费时长并保存
function getFreeTime($userId, $anchorId, $paycall, $time)
{
    // $payCallModel = new PayCallModel();
    $guardModel = new Guard();
    $free_video_time = MemcacheClient::getInstance()->get("guard_uid_" . $userId . "_user_id_" . $anchorId);
    $use_free_time = 0;
    if ($free_video_time !== false && $paycall['start_time'] > 0) {
        $call_time = $time - $paycall['start_time'];
        // $guard = $payCallModel->find("*", "uid = ? and user_id = ?", [$userId, $anchorId], '', "tb_guard");
        $guard = $guardModel->query()->where('uid', $userId)->where('user_id', $anchorId)->first();
        $use_free_time = $free_video_time - $call_time;
        if ($use_free_time > 0) {
            if ($guard['status'] == 1 && date('Y-m-d', $paycall['ctime']) == date('Y-m-d', time())) {
                // $payCallModel->update($guard['id'], ['free_time' => (int)$use_free_time], 'tb_guard');
                $guardModel::query()->where('id', $guard['id'])->update(['free_time' => (int)$use_free_time]);
            }
        } else {
            if (date('Y-m-d', $paycall['ctime']) == date('Y-m-d', time())) {
                // $payCallModel->update($guard['id'], ['free_time' => 0], 'tb_guard');
                $guardModel::query()->where('id', $guard['id'])->update(['free_time' => 0]);
            }
            MemcacheClient::getInstance()->delete("guard_uid_" . $userId . "_user_id_" . $anchorId);
        }
        // $payCallModel->update($paycall['id'], ['free_video_time' => (int)$use_free_time > 0 ? $call_time : $free_video_time]);// 通话记录保存该次通话免费时长
        PayCall::query()->where('id', $paycall['id'])->update(['free_video_time' => (int)$use_free_time > 0 ? $call_time : $free_video_time]);
        MemcacheClient::getInstance()->delete('the_pay_call_' . $paycall['id']);
    }
    return $use_free_time;
}


// 计算公爵免费时长并保存
function getSuperFreeTime($userId, $anchorId, $paycall, $time, $type = 1)
{
    $mem = MemcacheClient::getInstance();
    $userSuperVipModel = new UserSuperVip();
    $free_video_time = $mem->get("super_vip_uid_" . $userId . "_user_id_" . $anchorId);
    $use_free_time = 0;
    // $super = $payCallModel->find("*", "uid = ? and status = ?", [$userId, 1], '', "tb_user_super_vip");
    $super = $userSuperVipModel->query()->where('uid', $userId)->where('status', 1)->first();
    $deduct_time = $mem->get('deduct_time_' . $paycall['id']);
    if ($deduct_time === false) {
        if (!empty($super) && date('Y-m-d', $paycall['ctime']) == date('Y-m-d', time())) {
            // $payCallModel->update($super['id'], ['free_video_time' => (int)($super['free_video_time'] - 60)], 'tb_user_super_vip');
            $userSuperVipModel->query()->where('id', $super['id'])->update(['free_video_time' => (int)($super['free_video_time'] - 60)]);
            $mem->set('deduct_time_' . $paycall['id'], 60);
        }
    } else {
        if ($paycall['free_video_time'] > 0) {
            $call_time = $time - $paycall['start_time'] - $paycall['free_video_time'];
        } else {
            $call_time = $time - $paycall['start_time'];
        }
        $use_free_time = $free_video_time - $call_time;
        if ($use_free_time > 0) {
            if ($deduct_time <= $call_time) {
                $deduct_time = $deduct_time + 60;
                if (!empty($super) && date('Y-m-d', $paycall['ctime']) == date('Y-m-d', time())) {
                    // $payCallModel->update($super['id'], ['free_video_time' => (int)($super['free_video_time'] - 60)], 'tb_user_super_vip');
                    $userSuperVipModel->query()->where('id', $super['id'])->update(['free_video_time' => (int)($super['free_video_time'] - 60)]);
                    $mem->set('deduct_time_' . $paycall['id'], $deduct_time);
                }
            }
        } else {
            if (!empty($super) && date('Y-m-d', $paycall['ctime']) == date('Y-m-d', time())) {
                // $payCallModel->update($super['id'], ['free_video_time' => 0], 'tb_user_super_vip');
                $userSuperVipModel->query()->where('id', $super['id'])->update(['free_video_time' => 0]);
                $mem->set('deduct_time_' . $paycall['id'], $deduct_time);
            }
            $mem->delete("super_vip_uid_" . $userId . "_user_id_" . $anchorId);
        }
    }
    return $use_free_time;
}
